package com.ltx.controller;

import com.ltx.pojo.Kit;
import com.ltx.service.KitService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Kit")
public class KitController {
    //    controller 调用service层
    private final KitService kitService;
    public KitController(@Qualifier("kitServiceImpl") KitService kitService) {
        this.kitService = kitService;
    }


    //查询所有器材
    @RequestMapping("/allKit")
    public String list(Model model) {
        List<Kit> kits = kitService.selectAllKit();
        model.addAttribute("list",kits);
        return "/allKit";
    }
    //跳转到添加器材页面
    @RequestMapping("/toAddKit")
    public  String toAddKit(){
        return"/addKit";
    }
    //添加器材
    @RequestMapping("/addKit")
    public  String addKit(Kit kit){
        kitService.addKit(kit);
        return "redirect:/Kit/allKit";
    }
    //跳转到租借器材页面
    @RequestMapping("/toRuntKit")
    public  String toRuntKit(int kitId,Model model){
        Kit kit = kitService.queryById(kitId);
        model.addAttribute("kit",kit);
        return ("/runtKit");
    }

    //租借器材
    @RequestMapping("/runtKit")
    public  String runtKit(Kit kit){
        kitService.updateKit(kit);
        return "redirect:/Kit/allKit";
    }
    //归还器材
    @RequestMapping("/returnKit")
    public  String returnKit(int kitId){
        Kit kit = kitService.queryById(kitId);
        kitService.returnKit(kit);
        return "redirect:/Kit/allKit";
    }

    //查询未被租借器材
    @RequestMapping("/selectKit")
    public String noRunt(Model model) {
        List<Kit> kits = kitService.selectAllKit();
        List<Kit> list =new ArrayList<Kit>();
        for (Kit kit : kits) {
            if(kit.getmId()==0){
                list.add(kit);
            }
        }
        model.addAttribute("list",list);
        return "/allKit";
    }

}
